package service.vo.XMLStruts;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.batik.apps.rasterizer.Main;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import service.vo.XMLBean;

public class XMLStrutsVO {

	public XMLStrutsVO(){
		
	}
	
	public static Map<String,XMLBean> translateXML() throws Exception{
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build(new File("E:\\eclipsePro\\mvc1\\web\\WEB-INF\\struts-config.xml"));
			Element root = document.getRootElement();
			Map<String,XMLBean> actionMap = new HashMap<String,XMLBean>();
			Element actionroot = root.getChild("action-mapping");
			List<Element> aform = actionroot.getChildren();
			for(Element e: aform){
					XMLBean action = new XMLBean();
					String name = e.getAttributeValue("name");
					action.setBeanName(name);
					Element actionform = root.getChild("formbeans");
				     List<Element> formList=actionform.getChildren();
				     
				     for (Element element : formList) {
				    	 //�ҳ�formbean��name��action��һ�µ�Ԫ��
				    	 if(name.equals( element.getAttribute("name"))){
				    		 String formclass = element.getAttributeValue("class");
				    		 action.setFormClass(formclass);
				    		 break;
				    	 }
				    	
					}
				String path   = e.getAttributeValue("path");
				String type   = e.getAttributeValue("type");
				action.setPath(path);
				action.setActionClass(type);
				List<Element> forward = e.getChildren();
				Map<String,String> map = new HashMap<String,String>();
				for( Element x :forward){
					String fname = x.getAttributeValue("name");
					String fvalue = x.getAttributeValue("value");
					map.put(fname, fvalue);
					}
				action.setActionForward(map);
				actionMap.put(path, action);
			}
			//System.out.println(actionMap);
			return actionMap;
	}
	public static void main(String[] args) throws Exception
	{
		      Map<String, XMLBean> map=new XMLStrutsVO().translateXML();
		      Set<String> keyset = map.keySet();
		      for(Iterator<String> it = keyset.iterator();it.hasNext();)
		      {
		    	  String key =it.next();
		    	  System.out.println(key);
		    	  System.out.println(map.get(key));
		      }
	}
}
